<?php

namespace app\data\controller\report;

use think\admin\Controller;

/**
 * 数据统计-人力施工统计
 * Class StatisticsPainter
 * @package app\data\controller\report
 */
class StatisticsPainter extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'data_record';

    /**
     * 人力施工统计
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        // 设置页面标题
        $this->title = '人力施工统计';

        [$minDate, $maxDate] = $this->getTime();
        $result = $this->app->db->name($this->table)
            ->where('paint_status', 1)
            ->where('time_complete', '>=', $minDate)
            ->where('time_complete', '<=', $maxDate . " 23:59:59")
            ->select()->toArray();

        $painters = array_unique(array_column($result, 'painter'));
        foreach ($painters as $key => $painter) {
            if (empty($painter)) {
                unset($painters[$key]);
            }
        }
        foreach ($result as $datum) {
            $time = date('Y-m-d', strtotime($datum['time_complete']));
            if (isset($map[$datum['painter']][$time])) {
                $map[$datum['painter']][$time] = $map[$datum['painter']][$time] + 1;
            } else {
                $map[$datum['painter']][$time] = 1;
            }
        }

        $painters = array_merge([''], $painters);
        $list[] = $painters;

        for ($end = $maxDate; $end >= $minDate; $end = date('Y-m-d', strtotime("-1 days", strtotime($end)))) {

            foreach ($painters as $painter) {
                if (empty($painter)) {
                    $formatData[] = $end;
                } else {
                    $formatData[] = $map[$painter][$end] ?? 0;
                }

            }
            $list[] = $formatData ?? [];
            unset($formatData);
        }


        $rowTotal = [];
        $columnTotal = [];
        // 横向数据统计
        foreach ($list as $rowKey => $row) {
            if ($rowKey != 0) {
                $rowTotal[$rowKey] = 0;
                foreach ($row as $colKey => $value) {
                    if ($rowKey > 0 && $colKey > 0) {
                        $rowTotal[$rowKey] += $value;
                    }
                }
            }

        }
        // 纵向数据统计
        foreach ($list[0] as $colKey => $col) {
            $columnTotal[$colKey] = 0;
            foreach ($list as $rowKey => $row) {
                if ($rowKey > 0 && $colKey > 0) {
                    $columnTotal[$colKey] += $row[$colKey];
                }
            }
        }
        // 添加“总计”和“合计”
        $list[0][] = '总计';
        foreach ($rowTotal as $rowKey => $total) {
            $list[$rowKey][] = $total;
        }
        foreach ($columnTotal as $key => $total) {
            if (!$key) {
                $mergeData[] = '总计';
            } else {
                $mergeData[] = $total;
            }

        }
        $list[] = $mergeData ?? [];

        $n = count($list) - 1;
        $list[$n][] = array_sum($list[$n]);

        $this->list = $list;
        $this->fetch();
    }

    /**
     * 获取时间
     * @return array
     */
    private function getTime()
    {
        $date = request()->get('date');
        if (empty($date)) {
            for ($i = 1; $i <= 7; $i++) {
                $data[] = date('Y-m-d', strtotime("-$i days"));
            }
        } else {
            $date = explode(' - ', $date);
            if ((strtotime($date[1]) - strtotime($date[0])) > 3600 * 24 * 7) {
                $this->error('时间范围不能超过7天');
            }

            $firstDate = date('Y-m-d', strtotime($date[0]));
            $endDate = date('Y-m-d', strtotime($date[1]));

            for ($i = 0; $i < 7; $i++) {
                $data[] = date('Y-m-d', strtotime("-$i days", strtotime($endDate)));

                if (date('Y-m-d', strtotime("-$i days", strtotime($endDate))) == $firstDate) {
                    break;
                }
            }
        }

        return [min($data), max($data)];
    }
}